<template>
  <view class="container">
    <view class="navigation">
      <navigator url="/pages/recognize" class="nav-item">慧眼识图</navigator>
      <navigator url="/pages/bot" class="nav-item">链语AI</navigator>
      <navigator url="/pages/database" class="nav-item">藏药云库</navigator>
      <navigator url="/pages/databoard" class="nav-item">药海智屏</navigator>
    </view>

    <view class="main-content">
      <view class="content">
        <view class="result">{{ recognitionResult }}</view>
        <view class="image-preview">
          <image :src="imageSrc" mode="widthFix" v-if="imageSrc"></image>
        </view>
        <button @click="chooseImage">上传图片</button>
        <button @click="takePhoto" v-if="isMobileOrMiniProgram">拍照识别</button>
        <button @click="startRecognition">开始识别</button>
      </view>
    </view>
  </view>
</template>

<script>
import { ref, onMounted } from 'vue';
import { useDeviceType } from '@/utils/global.js'; // 检测设备类型的函数
import { handleRecognition } from '@/utils/recognize.js'; // 导入处理图片识别的函数

export default {
  setup() {
    const imageSrc = ref('');
    const recognitionResult = ref('识别的结果将展示在这里'); // 默认显示内容
    const isMobileOrMiniProgram = useDeviceType();

    const chooseImage = () => {
      uni.chooseImage({
        count: 1,
        success: (res) => {
          imageSrc.value = res.tempFilePaths[0];
        }
      });
    };

    const takePhoto = () => {
      uni.chooseImage({
        sourceType: ['camera'],
        count: 1,
        success: (res) => {
          imageSrc.value = res.tempFilePaths[0];
        }
      });
    };

    const startRecognition = () => {
      if (imageSrc.value) {
        recognitionResult.value = handleRecognition(imageSrc.value); // 调用处理图片识别的函数
      } else {
        recognitionResult.value = "请先上传图片";
      }
    };

    return {
      imageSrc,
      recognitionResult,
      isMobileOrMiniProgram,
      chooseImage,
      takePhoto,
      startRecognition
    };
  }
};
</script>

<style lang="scss" scoped>
@import '@/styles/global.scss';
@import '@/styles/recognize.scss';

.container {
  display: flex;
  flex-direction: column;
  height: 100vh; /* 占满整个视口高度 */
  overflow: hidden;
}

.navigation {
  display: flex;
  justify-content: space-around;
  background-color: #f8f8f8;
  padding: 10px 0;
  border-bottom: 1px solid #ddd;
  z-index: 1000; /* 确保导航栏在最上方 */
}

.nav-item {
  padding: 10px;
  margin: 0 10px;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.3s;

  &:hover {
    background-color: #e0e0e0;
  }
}

.main-content {
  flex: 1;
  overflow: hidden;
  padding: 20px;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.result {
  margin-top: 20px;
  padding: 10px;
  border: 1px solid #ddd;
  background-color: #f9f9f9;
  word-wrap: break-word;
  min-height: 100px; /* 确保文本框有一定的高度 */
  width: 100%;
  text-align: center;
}

.image-preview {
  width: 300px; /* 固定宽度 */
  height: 300px; /* 固定高度 */
  margin: auto;
  margin-bottom: 20px;
  position: relative;

  image {
    width: 100%;
    height: 100%;
    object-fit: contain; /* 保持图片比例并填充容器 */
  }
}

button {
  display: block;
  margin: 10px auto;
  padding: 10px 20px;
  background-color: #07c160;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;

  &:hover {
    background-color: #05a651;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content {
    flex-direction: column;
  }

  .image-preview {
    width: 100%;
  }
}
</style>